var images = new Array();
var titles = new Array();
var comments = new Array();
var ajax;

function loadData()
{   
    ajax = createAjaxObj();
    
    if(ajax)
    {
        ajax.onreadystatechange = function(){ parseFeed(); }
        ajax.open('GET', dataSourceFeed, true);
        ajax.send(null);
    }
    
}

function parseFeed()
{
    if (ajax.readyState == 4) //request completed
    { 
        if (ajax.status == 200) //request was successful
        { 
            var xmldata = ajax.responseXML;
            var items = xmldata.getElementsByTagName("item");

            for (var i = 0; i < items.length; i++)
            {
                var contentNode = items[i].getElementsByTagName("description")[0];
                var content = "";
                
                if(contentNode.nodeValue)
                {
                    content = contentNode.nodeValue;
                } else if(contentNode.firstChild && contentNode.firstChild.nodeValue) {
                    content = contentNode.firstChild.nodeValue;
                }
                
                /<img[^>]* src=\"([^\"]*)/.exec(content);
                if(RegExp.$1)
                {
                    images.push(RegExp.$1);
                    comments.push(trim(content.replace(/<[^>]*>/g, " ")));
                    
                    var title = items[i].getElementsByTagName("title")[0];
                    if(title != null)
                    {
                        if(title.nodeValue != null)
                        {
                            titles.push(trim(title.nodeValue));
                        } else if(title.firstChild && title.firstChild.nodeValue) {
                            titles.push(trim(title.firstChild.nodeValue));
                        } else {
                            titles.push("");
                        }
                        
                    }
                    else 
                    {
                        titles.push("");
                    }
                }
            }
        }
    }
    loadImages();
}

function createAjaxObj(){
    var httprequest = false;
    if (window.XMLHttpRequest) //Mozilla, Safari etc
    { 
        httprequest = new XMLHttpRequest();
        if(httprequest.overrideMimeType)
        {
            httprequest.overrideMimeType('text/xml');
        }
    }
    else if (window.ActiveXObject) //IE
    { 
        try 
        {
            httprequest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e)
        {
            try
            {
                httprequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e){}
        }
    }
    
    return httprequest;
}

function trim(s) 
{
    if(s)
    {
        while (s.substring(0,1) == ' ')
        {
            s = s.substring(1, s.length);
        }
        while (s.substring(s.length-1,s.length) == ' ') 
        {
            s = s.substring(0,s.length-1);
        }
        return s;
    }
    else
    {
        return "";
    }
}
